//
// video_select_server.h
//

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <video_server_common.h>
#include "uthash.h"

#define PORT "3490"   // port we're listening on
#define READ_BUFFER_SIZE 1024 * 1024

/*  ERROR DEFINITIONS  */
#define ERR_SERVER_NOT_INIT -2
#define ERR_SERVER_ALREADY_RUNNING -3
#define ERR_SERVER_NOT_RUNNING -4

// Represents a TCP video server
typedef struct video_server_t {
        int 				is_init;
        int 				is_running;
        int 				error_code;
        int				sockfd;
        fd_set 				master;    // master file descriptor list
        int 				fdmax;        // maximum file descriptor number
        int				listener;     // listening socket descriptor
        pthread_t 			server_thread; // the server's thread
	struct video_player_events_t 	*player_events;
        struct video_client_bind_t 	*client_bindings;
	char 				read_buffer[READ_BUFFER_SIZE];
	int 				bytes_read;
} video_server_t;

// C'tor
// player_events - delegates to handle connection/disconnection of video clients
void video_server(struct video_server_t *server, struct video_player_events_t *player_events);
// Starts a tcp video server
int video_server_start(struct video_server_t *server);
// Stops a running tcp video server
int video_server_stop(struct video_server_t *server);
// Destructor
int video_server_close(struct video_server_t *server);

